<template>
  <div id='content' class="content">
    <div id="article-list" v-if="tableArticle.length != 0">
      <!-- 游记列表... -->
      <div :class="{ article_item: true, with_cover: item.cover != null }" v-for="item in tableArticle" :key="item.article_id">
        <div class="title" @click="to_detail(item)">{{ item.title }}</div>
        <div class="abstract">{{ item.abstract }}</div>
        <div class="info">
          <span class="user-name">{{ item.user_all.user_name }}</span>
          <div class="footer-divider"></div>
          <span class="view">
            <i class="iconfont icon-liulan" />
            {{ item.views }}
          </span>
          <span>
            <i class="iconfont icon-dianzan-line" />
            {{ item.num_like.num }}
          </span>
          <span>
            <i class="iconfont icon-shoucang-line" />
            {{ item.num_collect.num }}
          </span>
          <span class="subject" v-if="item.article_subject != null">{{ item.article_subject.name }}</span>
          <span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
          <el-popover placement="bottom-start" width="150" v-model="item.popVisible">
            <el-button size="mini" type="danger" @click="deleteCollect(item)"><i class="el-icon-delete"></i>取消收藏</el-button>
            <i class="el-icon-more" slot="reference"></i>
          </el-popover>
        </div>
        <div class="cover" v-if="item.cover != null">
          <el-image style="width: 114px; height: 90px" :src="item.cover" :fit="'cover'" />
        </div>
      </div>
    </div>
    <el-pagination background layout="prev, pager, next" :page-size="pageSize" :total="totalSize" :current-page="pageNum" hide-on-single-page
      @current-change="handleCurrentChange">
    </el-pagination>
  </div>
</template>

<script>
import articleApi from "@/api/article"
import collectApi from "@/api/collect"

export default {
  data() {
    return {
      articleId: [],//用户的所有文章的简要列表
      tableArticle: [],//当前页的文章列表
      subjectList: [], //主题列表
      totalSize: 0,//总共有多少页
      pageNum: 1,//页码
      pageSize: 4,//每页有多少个文章
      popVisible: false,//操作栏是否可见
    }
  },
  methods: {
    //获取文章主题列表
    async getSubject() {
      this.subjectList=(await articleApi.getSubject()).data;
    },

    async getUserArticle() {
      this.articleId=(await articleApi.getListByCollect()).data;
      this.totalSize=this.articleId.length;
      this.tableArticle=[]
      this.tableArticle=this.articleId.slice((this.pageNum-1)*this.pageSize,this.pageNum*this.pageSize)
    },

    async handleCurrentChange(val) {
      this.tableArticle=[]
      this.pageNum=val;
      this.tableArticle=this.articleId.slice((val-1)*this.pageSize,val*this.pageSize)
    },

    //跳转到游记详情界面
    to_detail(item) {
      this.$router.push({
        name: '个人中心-文章详情',
        params: {
          article_id: item.article_id
        }
      })
    },

    async deleteCollect(item) {
      collectApi.changeCollect(item.article_id,'1');
      item.popVisible=false
      await this.getUserArticle();
      let query={ path: "/personal/collection/blogCollection" }
      this.$router.push({
        path: '/goto',
        query: query
      })
    },
  },
  mounted() {
    this.getUserArticle()
    this.getSubject()
  }
}
</script>

<style scoped lang='scss'>
.content {
  height: 728px;
  padding: 60px 0;
  background-color: rgba(34, 52, 87, 0.6);
}

#article-list {
  height: 550px;
  width: 1000px;
  padding: 16px 32px;
  margin: auto;
  background-color: rgba(34, 52, 87, 0.3);
  border-radius: 8px;
  user-select: none;
  cursor: pointer;
}

.article_item {
  position: relative;
  height: 120px;
  padding: 16px 20px 0;
  font-family: -apple-system, system-ui, Segoe UI, Roboto, Ubuntu, Cantarell,
    Noto Sans, sans-serif, BlinkMacSystemFont, Helvetica Neue, PingFang SC,
    Hiragino Sans GB, Microsoft YaHei, Arial !important;

  .title {
    display: -webkit-box;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    margin-bottom: 4px;
    font-weight: 500;
    font-size: 22px;
    line-height: 33px;
    letter-spacing: 1.5px;
    color: rgba(26, 143, 204, 1);
  }

  .abstract {
    display: -webkit-box;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    font-weight: 400;
    font-size: 18px;
    line-height: 30px;
    color: #b0c4de;
  }

  .info {
    font-weight: 400;
    font-size: 18px;
    line-height: 27px;
    color: #b0c4de;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    word-break: break-all;
    padding-bottom: 12px;
    border-bottom: 1px solid rgba(228, 230, 235, 0.5);

    .user-name {
      color: #5fa6c3;
    }

    .footer-divider {
      display: inline-block;
      width: 1px;
      height: 12px;
      background-color: #e4e6eb;
      margin: 4px 8px 0 8px;
    }

    .like {
      margin-left: 4px;
    }

    .collect {
      margin-left: 4px;
    }

    .subject {
      float: right;
      color: rgba(209, 215, 221, 0.8);
      letter-spacing: 1px;
      background-color: rgba(0, 151, 215, 0.8);
      padding: 0 6px;
      margin-top: 2px;
      border-radius: 2px;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
      font-size: 14px;
      min-height: 21px;
      line-height: 21px;
    }
  }

  .cover {
    position: absolute;
    top: 16px;
    right: 20px;

    img {
      display: inline;
      flex: 0 0 auto;
      width: 114px;
      height: 90px;
      margin-left: 24px;
      background-color: #fff;
      border-radius: 4px;
      border: 1px solid rgba(26, 160, 221, 0.5);
      overflow: clip;
      -o-object-fit: cover;
      object-fit: cover;
    }
  }
}

.with_cover {
  .title,
  .abstract,
  .info {
    padding-right: 160px !important;
  }
}

::v-deep .el-pagination {
  position: fixed;
  right: 24%;
  bottom: 8%;
}
</style>